﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Xml;

namespace LibGreen
{

	public abstract class Contact : IDialog
	{
		protected string displayName = null;
		private string handle = null;
		private string alias = null;
		private string email = null;
		private ContactStatus status;
		private string statusMessage = null;
		private DateTime? online = null;
		private DateTime? idleTime = null;
		private string profile = null;
		private Uri iconpath;

		public Uri IconUri
		{
			get { return iconpath; }
			set { iconpath = value; }
		}

		#region Properties
		/// <summary>
		/// The name of the contact displayed to the user
		/// </summary>
		public string DisplayName
		{
			get
			{
				return displayName;
			}
		}

		public string Alias
		{
			get 
			{
				string value = alias;
				if (string.IsNullOrEmpty(alias))
					value = Handle;
				return value; 
			}
			set { alias = value; }
		}



		/// <summary>
		/// Gets or sets the basic name of this Contact according to its protocol
		/// </summary>
		public string Handle
		{
			get { return (string.IsNullOrEmpty (handle) ? displayName : handle); }
			set
			{
				handle = value;
			}
		}
		
		/// <summary>
		/// Gets the email address associated with this contact
		/// </summary>
		public string Email
		{
			get { return email; }
			set { email = value; }
		}

		/// <summary>
		/// Gets the contact-defined status message
		/// </summary>
		public string StatusMessage
		{
			get { return statusMessage; }
			set { statusMessage = value; }
		}

		/// <summary>
		/// Gets the number of minutes which this contact has been idle
		/// </summary>
		public DateTime? IdleTime
		{
			get { return idleTime; }
			set { idleTime = value;	}
		}

		/// <summary>
		/// Gets the contact-defined profile information
		/// </summary>
		public string Profile
		{
			get { return profile; }
			set {profile = value; }
		}

		public DateTime? Online
		{
			get { return online; }
			set { online = value; }	
		}

		#endregion
		
		/// <summary>
		/// The contact is online
		/// </summary>
		public bool IsOnline
		{
			get { return Online != null; }
			set
			{
				if (value && Online != null)
					Online = DateTime.Now;
				else
					Online = null;
			}
		}

		/// <summary>
		/// The contact is away
		/// </summary>
		public bool IsAway
		{
			get { return (Online != null); }
			set
			{
				if (value && Online != null)
					Online = DateTime.Now;
				else
					Online = null;
			}
		}
		/// <summary>
		/// The contact is idle
		/// </summary>
		public bool IsIdle
		{
			get { return (IdleTime != null); }
			set 
			{
				if (value && IdleTime != null)
					IdleTime = DateTime.Now;
				else
					IdleTime = null;
			}
		}
		public bool IsMobile
		{
			get { return ((ContactStatus.Mobile & status) == ContactStatus.Mobile); }
			set 
			{ 
				if(value)
					status |= ContactStatus.Mobile;
				else
					status |= ~ContactStatus.Mobile;
			}
		}


		public override string ToString ()
		{
			return Alias;
		}

		#region ISessionIM Members

		public abstract event LibGreen.Events.MessageReceivedHandler MessageReceived;

		public abstract void SendMessage (Message mess);

		#endregion

		#region ISession Members

		public abstract event LibGreen.Events.SessionOpenHandler SessionOpen;

		public abstract event LibGreen.Events.SessionFailureHandler SessionFailure;

		public abstract void Open ();

		public abstract void Close ();

		/// <summary>
		/// The Clients login name
		/// </summary>
		public abstract string UserName
		{
			get;
		}


		#endregion
	}
}
